package com.pro01.backend.service;

import com.pro01.backend.mapper.Forum2CommentLikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Forum2CommentLikeService {

    @Autowired
    private Forum2CommentLikeMapper likeMapper;

    // 点赞评论
    public int likeComment(Long userId, Long commentId) {
        if (likeMapper.hasLiked(userId, commentId)) {
            throw new RuntimeException("您已点赞过该评论");
        }
        int inserted = likeMapper.insertLike(userId, commentId);
        if (inserted > 0) {
            likeMapper.incrementLikeCount(commentId);
        }
        return inserted;
    }

    // 取消点赞评论
    public int unlikeComment(Long userId, Long commentId) {
        if (!likeMapper.hasLiked(userId, commentId)) {
            throw new RuntimeException("您尚未点赞该评论");
        }
        int deleted = likeMapper.deleteLike(userId, commentId);
        if (deleted > 0) {
            likeMapper.decrementLikeCount(commentId);
        }
        return deleted;
    }
}

